(function product($) {

    global = {
        user:{}
    };
    window.onload = function () {
        var _this = this;
        Store.getUser().then(function (data) {
            if (data != null) {
                _this.global.user = data;
            }
        });
    }

    if (global.user != null || global.user.uid != "") {
        $(".addcar").click(function (event) {
            var offset = $("#fly-end").offset();
            var addcar = $(this);
            var img = $(".img-pro").attr('src');
            if(img == null){
                img = addcar.parent().parent().parent().find('img').attr('src');
                var flyer = $('<img z-index="99" class="u-flyer" src="' + img + '">');
                flyer.fly({
                    start: {
                        left: event.pageX,
                        top: event.pageY
                    },
                    end: {
                        left: offset.left,
                        top: 0,
                        width: 0,
                        height: 0
                    },
                    onEnd: function () {
                        $("#msg").show().animate({width: '250px'}, 200).fadeOut(500);
                        this.destory();
                    }
                });
            }else{
                var flyer = $('<img  z-index="9" class="u-flyer" src="' + img + '">');
                flyer.fly({
                    start: {
                        left: event.pageX,
                        top: event.pageY
                    },
                    end: {
                        left: offset.left,
                        top: offset.top,
                        width: 0,
                        height: 0
                    },
                    onEnd: function () {
                        $("#msg").show().animate({width: '250px'}, 200).fadeOut(500);
                        this.destory();
                    }
                });
            }


        });
    }


    add2cart = function (pid) {
        if (global.user == "null") {
            prompt("you didn't login,went login");
            window.setTimeout(function () {
                window.location.href = Store.context + "/user/gotologin.do";
            }, 1000);
        } else {
            $.ajax({
                url: Store.context + "/car/add2cart.do",
                type: "post",
                data: {
                    pid: pid,
                    count: $("#qty").val()
                },
                success: function (d) {
                    if (d.status == 1) {
                        prompt(d.msc, 900);
                        setTimeout(function () {
                            location.href = Store.context + "/gotologin.do";
                        })
                    }
                    if (d.status == 0) {
                        //
                        // prompt("正在前往购物车", 900);
                        // var c = 1;
                        // window.setInterval(function () {
                        //     c--;
                        //     if (c == 0) {
                        //         location.href = Store.context + "/car/showcar.do";
                        //     }
                        // }, 1000);
                    }
                }
            })
        }
    }

    addcart = function (pid) {
        if (global.user == "null") {
            prompt("you didn't login,went login");
            window.setTimeout(function () {
                window.location.href = Store.context + "/user/gotologin.do";
            }, 1000);
        } else {
            $.ajax({
                url: Store.context + "/car/add2cart.do",
                type: "post",
                data: {
                    pid: pid,
                },
                success: function (d) {
                    if (d.status == 1) {
                        prompt(d.msc, 900);
                        setTimeout(function () {
                            location.href = Store.context + "/user/gotologin.do";
                        })
                    }
                    if (d.status == 0) {
                        //
                        // prompt("正在前往购物车", 900);
                        // var c = 1;
                        // window.setInterval(function () {
                        //     c--;
                        //     if (c == 0) {
                        //         location.href = Store.context + "/car/showcar.do";
                        //     }
                        // }, 1000);
                    }
                }
            })
        }
    }

    createorder = function(pid) {
        $.ajax({
            url: Store.context + "/order/createOrder.do",
            type: "post",
            data: {
                pid: pid,
                count: $("#quantity").val()
            },
            success: function (d) {
                if (d.status == 0) {
                    location.href = Store.context + "/order/gotoOrderInfo.do";
                }
                if (d.status == 1) {
                    noinventory();
                }
            }
        })
    }

    noinventory = function() {
        prompt("库存不足", 900)
    };

    updateCollect = function(data, pid) {
        var _this = $(data);
        if (global.user == null) {
            prompt("you didn't login,went login");
            window.setTimeout(function () {
                window.location.href = Store.context +"/user/gotologin.do";
            }, 1000);
        } else {
            $.ajax({
                url: Store.context +"/product/updateCollect.do",
                type: "post",
                data: {
                    pid:pid
                },
                success: function (d) {
                    if (d.status == 1) {
                        prompt("正在前往登录", 900);
                        setTimeout(
                            function () {
                                window.location.href = Store.context +"/user/gotologin.do";
                            }, (1 * 1000));
                    }
                    if (d.status == 0) {
                        _this.toggleClass("btn-wish");
                        _this.toggleClass("btn-nowish");
                        if (d.data == 1) {
                            prompt("收藏商品成功", 800);
                        }
                        if (d.data == 0) {
                            prompt("已取消收藏", 900)
                        }
                    }
                }
            })
        }
    }

    soldOut = function () {
        var windowWidth = $(window).width();
        var tipsDiv = '<div class="tipsClass">' + "该商品已下架" + '</div>';
        $('body').append(tipsDiv);
        $('div.tipsClass').css({
            'top': 130 + 'px',
            'left': ( windowWidth / 2 ) - 350 / 2 + 'px',
            'position': 'fixed',
            'padding': '10px 16px',
            'background': "#FF0000",
            'font-size': 18 + 'px',
            'margin': '0 auto',
            'text-align': 'center',
            'width': '350px',
            'height': 'auto',
            'color': 'white',
            'opacity': '0.8'
        }).show();
        setTimeout(function () {
            $('div.tipsClass').fadeOut();
        }, 1000);
    }

    function prompt(text, _time) {
        var windowWidth = $(window).width();
        var tipsDiv = '<div class="tipsClass">' + text + '</div>';
        $('body').append(tipsDiv);
        $('div.tipsClass').css({
            'top': 130 + 'px',
            'left': ( windowWidth / 2 ) - 350 / 2 + 'px',
            'position': 'fixed',
            'padding': '10px 16px',
            'background': '#7FFFD4',
            'font-size': 18 + 'px',
            'margin': '0 auto',
            'text-align': 'center',
            'width': '350px',
            'height': 'auto',
            'color': 'white',
            'opacity': '0.8'
        }).show();
        setTimeout(function () {
            $('div.tipsClass').fadeOut();
        }, _time);
    }
}(jQuery));